package com.mmall.service.impl;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.mmall.common.RequestHolder;
import com.mmall.dao.SysRoleAclMapper;
import com.mmall.dao.SysRoleMapper;
import com.mmall.dao.SysRoleUserMapper;
import com.mmall.exception.ParamException;
import com.mmall.param.RoleParam;
import com.mmall.pojo.SysRole;
import com.mmall.service.ISysLogService;
import com.mmall.service.ISysRoleService;
import com.mmall.util.BeanValidatorUtil;
import com.mmall.util.IpUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author wangtengyu
 * @Create 2018-03-15-13:40
 */
@Service("iSysRoleService")
public class SysRoleServiceImpl implements ISysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    @Autowired
    private SysRoleAclMapper sysRoleAclMapper;

    @Autowired
    private ISysLogService iSysLogService;


    public void save(RoleParam param) {
        BeanValidatorUtil.check(param);
        if(checkExist(param.getName(),param.getId())){
            throw new ParamException("存在同名角色");
        }
        SysRole role=SysRole.builder().name(param.getName()).type(param.getType()).status(param.getStatus())
                .remark(param.getRemark()).build();
        role.setOperator(RequestHolder.getCurrentUser().getUsername());
        role.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
        role.setOperateTime(new Date());
        sysRoleMapper.insertSelective(role);
        iSysLogService.saveRoleLog(null,role);
    }


    public void update(RoleParam param) {
        BeanValidatorUtil.check(param);
        if(checkExist(param.getName(), param.getId())){
            throw new ParamException("存在同名角色");
        }
        SysRole before=sysRoleMapper.selectByPrimaryKey(param.getId());
        Preconditions.checkNotNull(before,"要更新的角色不存在");
        SysRole after=SysRole.builder().id(param.getId()).name(param.getName()).type(param.getType()).status(param.getStatus())
                .remark(param.getRemark()).build();
        after.setOperator(RequestHolder.getCurrentUser().getUsername());
        after.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
        after.setOperateTime(new Date());
        sysRoleMapper.updateByPrimaryKeySelective(after);
        iSysLogService.saveRoleLog(before,after);
    }


    @Transactional
    public void delete(int roleId){
        SysRole role=sysRoleMapper.selectByPrimaryKey(roleId);
        Preconditions.checkNotNull(role,"要删除的角色不存在");
        sysRoleMapper.deleteByPrimaryKey(roleId);
        sysRoleUserMapper.deleteByRoleId(roleId);
        sysRoleAclMapper.deleteByRoleId(roleId);
        iSysLogService.saveRoleLog(role,null);
    }

    /**
     * 根据权限id获得对应的角色
     * @param aclId
     * @return
     */
    public List<SysRole> getRoleListByAclId(int aclId){
        List<Integer> roleIdList=sysRoleAclMapper.getRoleIdListByAclId(aclId);
        if(CollectionUtils.isEmpty(roleIdList)){
            return Lists.newArrayList();
        }
        List<SysRole> roleList=sysRoleMapper.getByIdList(roleIdList);
        return roleList;


    }


    public List<SysRole> getRoleListByUserId(int userId){
        List<Integer> roleIdList=sysRoleUserMapper.getRoleIdListByUserId(userId);
        if(CollectionUtils.isEmpty(roleIdList)){
            return Lists.newArrayList();
        }
        return sysRoleMapper.getByIdList(roleIdList);
    }


    public List<SysRole> getAll(){
        List<SysRole> roleList=sysRoleMapper.selectAll();
        return roleList;
    }



    public boolean checkExist(String name,Integer id){
        return sysRoleMapper.countByNameAndId(name,id)>0;
    }
}
